﻿/// <reference path="jquery-2.0.2.js" />
/// <reference path="class.js" />
/// <reference path="http-requester.js" />

var persisters = (function () {

    var username = localStorage.getItem("username");

    function saveUserToLocalStorage(userData) {
        localStorage.setItem("username", userData.Username);
        username = userData.Username;
    }

    function clearUserFromLocalStorage() {
        localStorage.removeItem("username");
        username = "";
    }

    var MainPersister = Class.create({
        init: function (rootUrl) {
            this.rootUrl = rootUrl;
            this.user = new UserPersister(this.rootUrl);
            this.articles = new ArticlesPersister(this.rootUrl);
            this.comments = new CommentsPersister(this.rootUrl);
            this.votes = new VotesPersister(this.rootUrl);
        },
        isUserLoggedIn: function () {
            var isLoggedIn = username != null;
            return isLoggedIn;
        },
        userId: function () {
            return username;
        }
    });

    var UserPersister = Class.create({
        init: function (rootUrl) {
            this.rootUrl = rootUrl + "users/";
        },
        register: function (user, success, error) {
            var url = this.rootUrl + "0/register/";
            var userData = {
                Username: user.username,
                AuthKey: user.password
                //authCode: CryptoJS.SHA1(user.username + user.password).toString()
            };

            httpRequester.postJSON(
                url,
                userData,
				function (data) {
				    saveUserToLocalStorage(data);
				    success(data);
				},
                function (error) {
                    console.log(error)
                });
        },
        login: function (user, success, error) {
            var url = this.rootUrl + "0/login";
            var userData = {
                Username: user.username,
                AuthKey: user.password
                //authCode: CryptoJS.SHA1(user.username + user.password).toString()
            };
            console.log(userData);
            httpRequester.postJSON(
                url,
                userData,
                function (data) {
                    saveUserToLocalStorage(data);
                    success(data);
                },
                function (error) {
                    console.log(error);
                });
        },
        logout: function (success, error) {
            var url = this.rootUrl + username + "/logout";

            httpRequester.getJSON(
                url,
                function (data) {
                    clearUserFromLocalStorage();
                    success(data);
                },
                error)
        }
    });

    var ArticlesPersister = Class.create({
        init: function (url) {
            this.rootUrl = url + "articles";
        },
        create: function (article, success, error) {
            var articleData = {
                Title: article.Title,
                Text: article.Text,
                ImageUrl: article.ImageUrl,
                User: {
                    Username: localStorage["username"]
                },
                Comments: [],
                Votes: []
            }

            httpRequester.postJSON(
                this.rootUrl,
                articleData,
                success,
                error);
        },
        update: function (article, success, error) {
            var url = this.rootUrl + "/" + article.ID;

            var articleData = {
                ID: article.ID,
                Title: article.Title,
                Text: article.Text,
                ImageUrl: article.ImageUrl,
                User: {
                    Username: localStorage["username"]
                },
                Comments: article.Comments,
                Votes: article.Votes
            }

            httpRequester.postJSON(
                url,
                articleData,
                success,
                error);
        },
        remove: function (article, success, error) {
            var url = this.rootUrl + "/" + article.ID;

            httpRequester.getJSON(
                url,
                success,
                error);
        },
        get: function (articleID, success, error) {
            var url = this.rootUrl + "/" + articleID + "/details";

            httpRequester.getJSON(
                url,
                success,
                error);
        },
        getByPage: function (pageCount, success, error) {
            var url = this.rootUrl + "/page?p=" + pageCount;

            httpRequester.getJSON(
                url,
                success,
                error);
        },
        getAll: function (success, error) {
            httpRequester.getJSON(
                this.rootUrl,
                success,
                error);
        },
        getArticleComments: function (article, success, error) {
            var url = this.rootUrl + "/" + article.ID + "/comments";

            httpRequester.getJSON(
                url,
                success,
                error);
        },
        getArticleVotes: function (article, success, error) {
            var url = this.rootUrl + "/" + article.ID + "/votes";

            httpRequester.getJSON(
                url,
                success,
                error);
        }
    });

    var CommentsPersister = Class.create({
        init: function (url) {
            this.rootUrl = url + "comments";
        },
        create: function (comment, success, error) {
            var commentData = {
                Text: comment.Text,
                ParentComment: comment.ParentComment,
                User: {
                    Username: localStorage["username"]
                }
            }

            httpRequester.postJSON(
                this.rootUrl,
                commentData,
                success,
                error);
        },
        update: function (comment, success, error) {
            var url = this.rootUrl + "/" + comment.ID;

            var commentData = {
                ID: comment.ID,
                Text: comment.Text,
                ParentComment: comment.ParentComment,
                User: {
                    Username: localStorage["username"]
                }
            }

            httpRequester.postJSON(
                url,
                commentData,
                success,
                error);
        },
        remove: function (comment, success, error) {
            var url = this.rootUrl + "/" + comment.ID;

            httpRequester.getJSON(
                url,
                success,
                error);
        },
        getAll: function (success, error) {
            httpRequester.getJSON(
                this.rootUrl,
                success,
                error);
        }
    });

    var VotesPersister = Class.create({
        init: function (url) {
            this.rootUrl = url + "votes";
        },
        create: function (vote, success, error) {
            var voteData = {
                State: vote.State,
                User: vote.User,
                Article: vote.Article
            }

            httpRequester.postJSON(
                this.rootUrl,
                voteData,
                success,
                error);
        },
        update: function (vote, success, error) {
            var url = this.rootUrl + "/" + vote.ID;

            var voteData = {
                ID: vote.ID,
                State: vote.State,
                User: vote.User,
                Article: vote.Article
            }

            httpRequester.postJSON(
                url,
                voteData,
                success,
                error);
        }
    });

    //var GamePersister = Class.create({
    //    init: function (url) {
    //        this.rootUrl = url + "game/";
    //    },
    //    create: function (game, success, error) {
    //        var url = this.rootUrl + "create/" + sessionKey;

    //        var gameData = {
    //            title: game.title
    //        }
    //        if (game.password) {
    //            gameData.password = CryptoJS.SHA1(game.password).toString();
    //        }

    //        httpRequester.postJSON(url, gameData, success, error);
    //    },
    //    join: function (game, success, error) {
    //        var url = this.rootUrl + "join/" + sessionKey;

    //        var gameData = {
    //            id: game.id
    //        }
    //        if (game.password) {
    //            gameData.password = CryptoJS.SHA1(game.password).toString();
    //        }

    //        httpRequester.postJSON(url, gameData, success, error);
    //    },
    //    start: function (gameId, success, error) {
    //        var url = this.rootUrl + gameId + "/start/" + sessionKey;
    //        httpRequester.getJSON(url, success, error);
    //    },
    //    open: function (success, error) {
    //        var url = this.rootUrl + "open/" + sessionKey;
    //        httpRequester.getJSON(url, success, error);
    //    },
    //    myActive: function (success, error) {
    //        var url = this.rootUrl + "my-active/" + sessionKey;
    //        httpRequester.getJSON(url, success, error);
    //    },
    //    field: function (gameId, success, error) {
    //        var url = this.rootUrl + gameId + "/field/" + sessionKey;
    //        httpRequester.getJSON(url, success, error);
    //    }
    //});

    //var BattlePersister = Class.create({
    //    init: function (url) {
    //        this.rootUrl = url + "battle/";
    //    },
    //    move: function (gameId, unitInfo, success, error) {
    //        var url = this.rootUrl + gameId + "/move/" + sessionKey;
    //        httpRequester.postJSON(url, unitInfo, success, error);
    //    },
    //    attack: function (gameId, unitInfo, success, error) {
    //        var url = this.rootUrl + gameId + "/attack/" + sessionKey;
    //        httpRequester.postJSON(url, unitInfo, success, error);
    //    },
    //    defend: function (gameId, unitInfo, success, error) {
    //        var url = this.rootUrl + gameId + "/attack/" + sessionKey;
    //        var unitId = unitInfo.id;
    //        httpRequester.postJSON(url, unitId, success, error);
    //    }
    //});

    //var MessagesPersister = Class.create({
    //    init: function (url) {
    //        this.rootUrl = url + "messages/";
    //    },
    //    unread: function (success, error) {
    //        var url = this.rootUrl + "unread/" + sessionKey;
    //        httpRequester.getJSON(url, success, error);
    //    },
    //    all: function (success, error) {
    //        var url = this.rootUrl + "all/" + sessionKey;
    //        httpRequester.getJSON(url, success, error);
    //    }
    //});

    return {
        get: function (url) {
            return new MainPersister(url);
        }
    };

}());